/**
 * 管理首页，代理审核
 */
v_agent_audit = new Vue({
	el: '#agentAuditPage',
	data: {
		title: '代理商审核',
		needBack: false,
		needRefresh: true,
		navs: [{
			title: '代理商审核',
			icon: 'icon-tuandui',
			url: '/admin',
			active: 'active',
			badge: 0
		}, {
			title: '商品管理',
			icon: 'icon-shangpin',
			url: '/admin/goods',
			active: '',
			badge: 0
		}, {
			title: '订单管理',
			icon: 'icon-dingdan',
			url: '/admin/order',
			active: '',
			badge: 0
		}],
		mobileNo: '',
		agents: [],
		loading: false,
		pageNo: 1,
		totalPage: 100
	},
	created: function() {
		this.count();
		this.searchSubAgent();
		this.$nextTick(function() {
			var containerHeight = $('div.infinite-scroll').height();
			$('div.infinite-scroll').scroll(function(){
				//判断是否滑动到页面底部
				var $loading = $('div.infinite-scroll-preloader').offset();
				
				if ($loading.top <= 580){
					// 如果正在加载，则退出
			    	if (v_agent_audit.loading) {
			    		return;
			    	}
			    	// 设置flag
			    	v_agent_audit.loading = true;
			    	// 加页码
			    	v_agent_audit.pageNo ++;
			    	if (v_agent_audit.pageNo > v_agent_audit.totalPage) {
			    		$('div.infinite-scroll-preloader div.preloader')
			    		.removeClass('preloader')
			    		.css({'color': '#ccc', 'font-size': '0.7rem'})
			    		.html('----------我是有底线的----------');
			    		$('div.infinite-scroll').off('scroll');
					} else {
						v_agent_audit.searchSubAgent(v_agent_audit.pageNo, true);
					}
				}
			});
		});
	},
	methods: {
		count: function() {
			axios.post('/agent/audit/count')
			.then(function(resp) {
				if (resp.data.success) {
					v_agent_audit.navs[0].badge = resp.data.message;
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
			axios.post('/order/count')
			.then(function(resp) {
				if (resp.data.success) {
					var count = 0;
					$.each(resp.data.message, function(key, value) {
						count += Number(value);
					});
					v_agent_audit.navs[1].badge = count;
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		},
		logout: function() {
			self.location.href = '/logout';
		},
		searchAgent: function() {
			this.searchSubAgent(1);
		},
		searchSubAgent: function(page, append) {
			var param = {
				page_pageNo: page || 1,
				page_pageSize: 5,
				page_sort: 'joinTime_asc',
				filter_EQ_auditStatus: false,
				filter_LIKE_mobileNo: this.mobileNo
			};
			axios.post('/agent/list', param)
			.then(function(resp) {
				if (resp.data.success) {
					v_agent_audit.totalPage = resp.data.totalPage;
					v_agent_audit.pageNo = resp.data.pageNo + 1;
					if (append) {
						$.each(resp.data.rows, function(i, row) {
							v_agent_audit.agents.push(row);
						});
					} else {
						v_agent_audit.agents = resp.data.rows;
					}
					v_agent_audit.$nextTick(function() {
						v_agent_audit.loading = false;
					});
				}
			})
			.catch(function (error) {
			    console.log(error);
			});
		},
		audit: function(agent) {
			$.modal({
				title: '审核' + agent.name + '的代理商资格',
				text: '您是否要通过“' + agent.name + '”成为代理商的申请？注意线下加微信和加入门槛是否达标',
				buttons: [{
					text: '通过',
					onClick: function() {
						axios.post('/agent/audit/' + agent.id)
						.then(function(resp) {
							if (resp.data.success) {
								$.toast('审核成功。');
								v_agent_audit.searchSubAgent();
								v_agent_audit.count();
							}
						})
						.catch(function (error) {
						    console.log(error);
						});
					}
				},{
					text: '再想想'
				}]
			});
		},
		showDetail: function(agent) {
			v_agent_audit_detail.agent = agent;
			v_agent_audit_detail.$nextTick(function() {
				if (agent.hasAccount) {
					$.router.load('#agentDetailPage');
				} else {
					axios.post('/agent/account/show/' + agent.id)
					.then(function(resp) {
						if (resp.data.success) {
							agent.hasAccount = true;
							$.each(resp.data.message, function(type, account) {
								if (type == 'BANK') {
									v_agent_audit_detail.agent.bank = account.bank;
									v_agent_audit_detail.agent.bankCard = account.accountNo;
								} else if (type == 'ZHIFUBAO') {
									v_agent_audit_detail.agent.zhifubao = account.accountNo;
								} else if (type == 'WEIXIN') {
									v_agent_audit_detail.agent.wxpay = account.accountNo;
								}
							});
							v_agent_audit_detail.$forceUpdate();
							$.router.load('#agentDetailPage');
						}
					})
					.catch(function (error) {
					    console.log(error);
					});
				}
			});
		}
	}
});

v_agent_audit_detail = new Vue({
	el: '#agentDetailPage',
	data: {
		title: '代理商详情',
		needBack: true,
		needRefresh: false,
		agent: {},
		agentTitles: [{
			name: '姓名',
		},{
			sex_fmt: '性别',
		},{
			mobileNo: '手机号码',
		},{
			email: '邮箱',
		},{
			qq: 'QQ号码',
		},{
			signature: '签名',
		},{
			level_fmt: '代理等级',
		},{
			joinTime_fmt: '加入时间',
		},{
			bank: '银行',
		},{
			bankCard: '银行卡号',
		},{
			zhifubao: '支付宝账号',
		},{
			wxpay: '微信账号',
		}]
	},
	methods: {
		goback: function() {
			
		}
	}
});